using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Diagnostics;

namespace SBPweb.Controls.Windows.Editors.HelperClasses
{
	public class MyDataObject : IDataObject
	{
		private Bitmap mBitmap;
		public FORMATETC mpFormatetc;

		#region IDataObject Members

		private const uint S_OK = 0;
		private const uint E_POINTER = 0x80004003;
		private const uint E_NOTIMPL = 0x80004001;
		private const uint E_FAIL = 0x80004005;

		public uint GetData(ref FORMATETC pFormatetc, ref STGMEDIUM pMedium)
		{
			IntPtr hDst = mBitmap.GetHbitmap();

			pMedium.tymed = (int)TYMED.TYMED_GDI;
			pMedium.unionmember = hDst;
			pMedium.pUnkForRelease = IntPtr.Zero;

			return (uint)S_OK;
		}

		public uint GetDataHere(ref FORMATETC pFormatetc, out STGMEDIUM pMedium)
		{
			Trace.WriteLine("GetDataHere");

			pMedium = new STGMEDIUM();

			return (uint)E_NOTIMPL;
		}

		public uint QueryGetData(ref FORMATETC pFormatetc)
		{
			Trace.WriteLine("QueryGetData");

			return (uint)E_NOTIMPL;
		}

		public uint GetCanonicalFormatEtc(ref FORMATETC pFormatetcIn, out FORMATETC pFormatetcOut)
		{
			Trace.WriteLine("GetCanonicalFormatEtc");

			pFormatetcOut = new FORMATETC();

			return (uint)E_NOTIMPL;
		}

		public uint SetData(ref FORMATETC a, ref STGMEDIUM b, bool fRelease)
		{
			//mpFormatetc = pFormatectIn;
			//mpmedium = pmedium;

			Trace.WriteLine("SetData");

			return (int)S_OK;
		}

		public uint EnumFormatEtc(uint dwDirection, IEnumFORMATETC penum)
		{
			Trace.WriteLine("EnumFormatEtc");

			return (int)S_OK;
		}

		public uint DAdvise(ref FORMATETC a, int advf, IAdviseSink pAdvSink, out uint pdwConnection)
		{
			Trace.WriteLine("DAdvise");

			pdwConnection = 0;

			return (uint)E_NOTIMPL;
		}

		public uint DUnadvise(uint dwConnection)
		{
			Trace.WriteLine("DUnadvise");

			return (uint)E_NOTIMPL;
		}

		public uint EnumDAdvise(out IEnumSTATDATA ppenumAdvise)
		{
			Trace.WriteLine("EnumDAdvise");

			ppenumAdvise = null;

			return (uint)E_NOTIMPL;
		}

		#endregion

		public MyDataObject()
		{
			mBitmap = new Bitmap(16, 16);
			mpFormatetc = new FORMATETC();
		}

		public void SetImage(string strFilename)
		{
			try
			{
				mBitmap = (Bitmap)Bitmap.FromFile(strFilename, true);

				mpFormatetc.cfFormat = CLIPFORMAT.CF_BITMAP;				// Clipboard format = CF_BITMAP
				mpFormatetc.ptd = IntPtr.Zero;							// Target Device = Screen
				mpFormatetc.dwAspect = DVASPECT.DVASPECT_CONTENT;			// Level of detail = Full content
				mpFormatetc.lindex = -1;							// Index = Not applicaple
				mpFormatetc.tymed = TYMED.TYMED_GDI;					// Storage medium = HBITMAP handle
			}
			catch
			{
			}
		}

		public void SetImage(Image image)
		{
			try
			{
				mBitmap = new Bitmap(image);

				mpFormatetc.cfFormat = CLIPFORMAT.CF_BITMAP;				// Clipboard format = CF_BITMAP
				mpFormatetc.ptd = IntPtr.Zero;							// Target Device = Screen
				mpFormatetc.dwAspect = DVASPECT.DVASPECT_CONTENT;			// Level of detail = Full content
				mpFormatetc.lindex = -1;							// Index = Not applicaple
				mpFormatetc.tymed = TYMED.TYMED_GDI;					// Storage medium = HBITMAP handle
			}
			catch
			{
			}
		}
	}
}
